class <%= prefixed_controller_class_name %>Controller < <%= parent_controller_class_name %>Controller
  include ResourceControllerMixin

  def index
    @<%= plural_name %> = ::<%= orm_class.all(class_name) %>

    # if @query
    #  @<%= plural_name %> = @<%= plural_name %>.where("#{plural_table_name}.name LIKE ?", "%#{@query}%")
    # end

    <%- if active_enabled? %>
    if params[:active] == 'false'
      @<%= plural_name %> = @<%= plural_name %>.where(active: false)
    else
      @<%= plural_name %> = @<%= plural_name %>.where(active: true)
    end
    <%- end %>

    <%- if recommend_enabled? %>
    if params[:recommended] == 'true'
      @<%= plural_name %> = @<%= plural_name %>.where(recommended: true)
    elsif params[:recommended] == 'false'
      @<%= plural_name %> = @<%= plural_name %>.where(recommended: false)
    end
    <%- end %>

    @<%= plural_name %> = @<%= plural_name %>.page(params[:page]).per(page_size)

  end

  def new
  @<%= singular_name %> = ::<%= orm_class.build(class_name) %>
  end

  <%- unless simple_mode? %>
  def show
    if request.xhr?
      render partial: 'detail', locals: {<%= singular_name %>: @<%= singular_name %>}
    end
  end

  <%- end %>
  private
  def set_dirs
    @dir1 = '<%= singular_name %>'
  end

  def model
    ::<%= class_name %>
  end

  def set_resource
    @<%= singular_name %> = model.find(params[:id] || params[:<%= singular_name %>_id])
  end

  def resource
    @<%= singular_name %>
  end

  def build_resource
    @<%= singular_name %> = ::<%= class_name %>.new(resource_params)
  end

  def resource_params
  <%- if attributes_names.empty? -%>
    params[<%= ":#{singular_name}" %>]
  <%- else -%>
    params.require(<%= ":#{singular_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
  <%- end -%>
  end

  def redirect_to_after_save
    <%= "[:#{prefix}, :#{plural_table_name}]" %>
  end
end
